//
//  BasePanel.swift
//  SBSafetyApp
//
//  Created by Aimeow on 2022/11/28.
//

import Foundation
import UIKit
#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif

class WeekModel {
    var text: String = ""
    var value: String = ""
    init(text: String, value: String) {
        self.text = text
        self.value = value
    }
}

class EditBizDateView: UIView {
    var selectWeekDays: Set<String> = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
    var disposeBag = DisposeBag()
    
    let arr: [WeekModel] = [
        WeekModel(text: "周一", value: "星期一"), WeekModel(text: "周二", value: "星期二"), WeekModel(text: "周三", value: "星期三"),
        WeekModel(text: "周四", value: "星期四"), WeekModel(text: "周五", value: "星期五"), WeekModel(text: "周六", value: "星期六"),
        WeekModel(text: "周日", value: "星期日")]
    
    @objc func skipSwitchTapped(_ sender: UISwitch) {
        if sender.isOn {
            selectWeekDays = ["星期一", "星期二", "星期三", "星期四", "星期五"]
            checkBoxView.reloadData()
        } else {
            selectWeekDays = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
            checkBoxView.reloadData()
        }
    }

    lazy var commentLabel: UILabel = {
        let label = UILabel()
        label.textColor = .hex("#717D8F")
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.font = UIFont.systemFont(ofSize: 14)
        label.text = "说明：勾选上的营业时间表均为开门营业的日期，如遇节假日或特殊例外的日期，可以单独设置营业或者不营业。"
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.textColor = .hex("#306EC8")
        label.font = UIFont.systemFont(ofSize: 15)
        label.text = "选中即为营业"
        return label
    }()
    
    lazy var checkBoxView : UICollectionView = {
        let collection = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
        collection.delegate = self
        collection.dataSource = self
        collection.backgroundColor = .white
        collection.register(CheckBoxViewCell.self, forCellWithReuseIdentifier: "CheckBoxViewCell")
        return collection
    }()
    
    lazy var skipLabel: UILabel = {
        let label = UILabel()
        label.textColor = .hex("#000000")
        label.font = UIFont.systemFont(ofSize: 17)
        label.text = "智能跳过法定节假日"
        return label
    }()
    
    lazy var skipSwitchButton: UISwitch = {
        let switchBtn = UISwitch()
        return switchBtn
    }()
    
    init(withStyle style: SectionTitleStyleEnum, withTitle title: String) {
        super.init(frame: .zero)
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 5
        backgroundColor = .white
        
        let titleView = SectionTitleView(withStyle: style, title: title)
        let line = UIView.createLightLine()
        addSubview(titleView)
        addSubview(detailLabel)
        addSubview(line)
        addSubview(checkBoxView)
        addSubview(skipLabel)
        addSubview(skipSwitchButton)
        addSubview(commentLabel)
        
    
        titleView.snp.makeConstraints { make in
            make.left.top.equalTo(self).offset(20)
            make.top.equalTo(self).offset(20)
        }
        
        detailLabel.snp.makeConstraints { make in
            make.right.equalTo(self).offset(-18)
            make.centerY.equalTo(titleView)
        }
        
        line.snp.makeConstraints { make in
            make.top.equalTo(detailLabel.snp.bottom).offset(18)
            make.height.equalTo(0.5)
            make.left.equalTo(self).offset(20)
            make.right.equalTo(self).offset(-20)
        }
        
        checkBoxView.snp.makeConstraints { make in
            make.left.equalTo(self).offset(30)
            make.right.equalTo(self).offset(-30)
            make.top.equalTo(line.snp.bottom).offset(30)
            make.height.equalTo(150)
        }
        
        skipLabel.snp.makeConstraints { make in
            make.top.equalTo(checkBoxView.snp.bottom).offset(10)
            make.left.equalTo(self).offset(20)
        }
        
        skipSwitchButton.addTarget(self, action: #selector(skipSwitchTapped), for: .valueChanged)
        skipSwitchButton.snp.makeConstraints { make in
            make.right.equalTo(self).offset(-20)
            make.centerY.equalTo(skipLabel)
        }
        
        commentLabel.snp.makeConstraints { make in
            make.top.equalTo(skipLabel.snp.bottom).offset(32)
            make.left.equalTo(self).offset(20)
            make.right.equalTo(self).offset(-20)
            make.bottom.equalTo(self).offset(-30)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension EditBizDateView: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (ScreenWidth - 120) / 3.0, height: 20)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CheckBoxViewCell", for: indexPath) as! CheckBoxViewCell
        let data = arr[indexPath.item]

        cell.checkbox.buildData(title: data.text, enable: selectWeekDays.contains(data.value))
        cell.checkbox.rx.tapGesture().when(.recognized)
                .subscribe(onNext: { [self] _ in
                    cell.checkbox.buildData(title: data.text, enable: !cell.checkbox.isSelect)
                    if cell.checkbox.isSelect {
                        if !selectWeekDays.contains(data.value) {
                            selectWeekDays.insert(data.value)
                        }
                    } else {
                        selectWeekDays.remove(data.value)
                    }
                })
                .disposed(by: disposeBag)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
}


class CheckBoxViewCell: UICollectionViewCell {
    let checkbox = BSCheckbox()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(checkbox)
        checkbox.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
